package com.dengcj.epl.service.util;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import com.dengcj.epl.dao.DB;
import com.dengcj.epl.entity.ecology.hrm.Subcompany;
import com.dengcj.epl.entity.ecology.hrm.User;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dengchunjie
 * @Description: 组织架构人力资源相关工具类
 * @date 2022/1/15 12:27
 */
public class HrmUtil {

    /**
     * 获取用户信息
     * @param id 用户ID
     * @return
     */
    public static User getUser(int id){
        DB db = new DB();

        Entity entity = db.queryOne("select a.id,a.lastname,a.sex,a.mobile,a.email,a.status as statusIndex,a.departmentId,b.departmentName,a.jobtitle as jobtitleId,d.jobtitleName,c.subcompanyName,a.subcompanyid1 as subcompanyId,a.seclevel,a.managerId,e.lastname as managerName,a.loginId from hrmresource a " +
                "left join HrmDepartment b on b.id=a.departmentid " +
                "left join HrmSubCompany c on c.id=a.subcompanyid1 " +
                "left join HrmJobTitles d on d.id=a.jobtitle " +
                "left join hrmresource e on e.id=a.managerid where a.id=?", id);
        return Convert.convert(User.class, entity);
    }

    /**
     * 获取人员属性
     * @param id 用户ID
     * @return
     */
    public static String getUserAttr(int id,String attr){
        //增加非空判断
        if(StrUtil.isBlank(attr)){
            return "";
        }

        DB db = new DB();
        Entity entity = db.queryOne("select " + attr + " from hrmresource where id=?", id);
        return entity == null ? "" : entity.getStr(attr);
    }

    /**
     * 获取员工办公地点的名称
     * @param uid
     * @return
     */
    public static String getUserLocationName(String uid){
        DB db = new DB();
        Entity entity = db.queryOne("select b.locationname from hrmresource a inner join HrmLocations b on a.locationid=b.id where a.id=?", uid);
        return entity == null ? "" : entity.getStr("locationname");
    }

    /**
     * 获取部门自定义属性
     * @param deptid
     * @param attr
     * @return
     */
    public static String getDepartmentAttr(String deptid, String attr){
        DB db = new DB();
        Entity entity = db.queryOne("select "+attr+" from HrmDepartmentDefined where deptid=?", deptid);
        return entity == null ? "" : entity.getStr(attr);
    }


    /**
     * 获取分部属性
     * @param subcompanyid
     * @param attr
     * @return
     */
    public static String getCompanyAttr(String subcompanyid,String attr){
        DB db = new DB();
        Entity entity = db.queryOne("select "+attr+" from HrmSubcompanyDefined where subcomid=?", subcompanyid);
        return entity == null ? "" : entity.getStr(attr);
    }

    /**
     * 获取分部信息, 可以自动识别行政组织架构和虚拟组织架构
     * @param subcompanyId
     * @return
     */
    public static Subcompany getSubcompanyById(int subcompanyId){
        DB db = new DB();
        Entity entity = null;
        if(subcompanyId > 1){ // 行政组织架构
            entity = db.queryOne("select subcompanyname,subcompanycode from hrmsubcompany where id=?", subcompanyId);
        } else if(subcompanyId < 1){ // 虚拟组织架构
            entity = db.queryOne("select subcompanycode,subcompanyname from HrmSubCompanyVirtual where id=?", subcompanyId);
        }
        if(entity != null){
            Subcompany subcompany = new Subcompany();
            subcompany.setName(entity.getStr("subcompanyname"));
            subcompany.setCode(entity.getStr("subcompanycode"));

            return subcompany;
        }

        return null;
    }


    /**
     * 根据人员id获取人员姓名
     * @param uid
     * @return
     */
    public static String getUserLastname(int uid){
        DB db = new DB();
        Entity entity = db.queryOne("select lastname from hrmresource where id=?", uid);
        return entity == null ? "" : entity.getStr("lastname");
    }

    /**
     * 根据人员id获取部门ID
     * @param uid
     * @return
     */
    public static int getUserDeptId(int uid){
        DB db = new DB();
        Entity entity = db.queryOne("select departmentid from hrmresource where id=?", uid);
        return entity == null ? -1 : entity.getInt("departmentid");
    }

    /**
     * 根据人员id获取部门名称
     * @param uid
     * @return
     */
    public static String getUserDeptName(int uid){
        DB db = new DB();
        Entity entity = db.queryOne("select departmentname from hrmdepartment a inner join hrmresource b on a.id=b.departmentid where b.id=?", uid);
        return entity == null ? "" : entity.getStr("departmentname");
    }

    /**
     * 根据人员id获取人员loginid
     * @param uid
     * @return
     */
    public static String getUserLoginId(int uid){
        DB db = new DB();
        Entity entity = db.queryOne("select loginid from hrmresource where id=?", uid);
        return entity == null ? "" : entity.getStr("loginid");
    }

    /**
     * 根据人员id获取人员loginid
     * @param uids
     * @return
     */
    public static List<Entity> getUserLoginIds(String uids){
        DB db = new DB();
        List<Entity> entities = db.query("select loginid from hrmresource where id in(" + uids + ")");

        return entities;
    }

    /**
     * 根据人员loginid获取人员id
     * @param loginid
     * @return
     */
    public int getUidByLoginid(String loginid){
        DB db = new DB();
        Entity entity = db.queryOne("select id from hrmresource where loginid=?", loginid);
        return entity == null ? -1 : entity.getInt("id");
    }

    /**
     * 根据人员id获取分部ID
     * @param uid
     * @return
     */
    public static int getUserSubcompanyId(int uid){
        DB db = new DB();
        Entity entity = db.queryOne("select subcompanyid1 from hrmresource where id=?", uid);
        return entity == null ? -1 : entity.getInt("subcompanyid1");
    }

    /**
     * 根据部门id获取部门名称
     * @param id
     * @return
     */
    public static String getDepartmentName(int id){
        DB db = new DB();
        Entity entity = db.queryOne("select departmentname from hrmdepartment where id=?", id);
        return entity == null ? "" : entity.getStr("departmentname");
    }

    /**
     * 根据部门id判断该部门是否是末级部门
     * @param detpId
     * @return
     */
    public static boolean isLeafDept(int detpId){
        DB db = new DB();
        Entity entity = db.queryOne("select count(*) as rowCount from hrmdepartment where supdepid=?", detpId);
        return entity.getInt("rowCount") == 0;
    }

    /**
     * 根据部门id判断该部门是否是顶级部门
     * @param detpId
     * @return
     */
    public static boolean isTopDept(int detpId){
        DB db = new DB();
        Entity entity = db.queryOne("select supdepid from hrmdepartment where id=?", detpId);
        return entity == null ? false : entity.getInt("supdepid") == 0;
    }

    /**
     * 根据分部id获取分部名称
     * @param id
     * @return
     */
    public static String getSubcompanyName(int id){
        DB db = new DB();
        Entity entity = db.queryOne("select subcompanyname from hrmsubcompany where id=?", id);
        return entity == null ? "" : entity.getStr("subcompanyname");
    }

    /**
     * 获取部门的一级部门
     * @param deptid 部门id
     * @return 一级部门id
     */
    public static int getSupDept(int deptid){
        DB db = new DB();
        Entity entity = db.queryOne("select id,supdepid from hrmdepartment where id=?", deptid);
        return entity == null ? -1 : entity.getInt("supdepid");
    }

    /**
     * 获取部门的一级部门信息
     * @param deptid
     * @return  deptMap
     */
    public static Entity getSupDeptInfo(int deptid){
        List<Entity> deptAllLevel = getDeptAllLevel(deptid);

        if(deptAllLevel.size() > 0){
            return deptAllLevel.get(0);
        }

        return null;
    }

    /**
     * 获取部门的所有层级
     * @param deptid
     * @return
     */
    public static List<Entity> getDeptAllLevel(int deptid){
        List<Entity> deptList = new ArrayList();
        DB db = new DB();
        while(deptid != 0) {
            Entity entity = db.queryOne("select id,departmentname as name,supdepid from hrmdepartment where id=?", deptid);
            deptid = entity.getInt("supdepid");
            deptList.add(entity);
        }

        return ListUtil.reverse(deptList);
    }



    /**
     * 获取部门的所有层级字符串
     * @param deptid
     * @return
     */
    public static String getDeptAllLevelStr(int deptid){
        List<Entity> entities = getDeptAllLevel(deptid);
        String deptStr = entities.stream().map(entity -> entity.getStr("name")).collect(Collectors.toList()).stream().collect(Collectors.joining("/", "/",""));

        return deptStr;
    }

    /**
     * 获取用户的所有层级字符串（分部+部门）
     * 返回格式：XXX集团/行政部
     * @param userid
     * @return
     */
    public static String getUserOrgStr(int userid){
        DB db = new DB();
        Entity userEntity = db.queryOne("select departmentid,subcompanyid1 from hrmresource where id=?", userid);
        int deptId = userEntity.getInt("departmentid");
        int subcompanyId = userEntity.getInt("subcompanyid1");

        String subcompanyName = getSubcompanyName(subcompanyId);

        return subcompanyName+getDeptAllLevelStr(deptId);
    }

    /**
     * 根据人员id字符串获取人员姓名字符串
     * @param ids
     * @return
     */
    public static String getUserLastnames(String ids){
        //增加非空判断
        if("".equals(ids)){
            return "";
        }

        DB db = new DB();
        List<Entity> entities = db.query("select lastname from hrmresource where id in("+ids+")");
        String names = entities.stream().map(entity -> entity.getStr("lastname")).collect(Collectors.toList())
                .stream().distinct().collect(Collectors.joining(","));

        return names;
    }


    /**
     * 判断部门是否是该分部下的部门
     * @param subcompanyid 分部id
     * @param deptid   部门id
     * @return
     */
    public static boolean isSubcompanyDept(String subcompanyid,String deptid){
        //增加非空判断
        if("".equals(subcompanyid) || "".equals(deptid)){
            return false;
        }

        DB db = new DB();
        Entity entity = db.queryOne("select count(id) as rowCount from HrmDepartment where Subcompanyid1=? and id=?", subcompanyid, deptid);
        return entity.getInt("rowCount") > 0;
    }

    /**
     * 判断人员是否是该分部下的人员
     * @param subcompanyid 分部id
     * @param uid   部门id
     * @return
     */
    public static boolean isSubcompanyUser(String subcompanyid,String uid){
        //增加非空判断
        if("".equals(subcompanyid) || "".equals(uid)){
            return false;
        }

        DB db = new DB();
        Entity entity = db.queryOne("select count(id) as rowCount from hrmresource where subcompanyid1="+subcompanyid+" and id="+uid);
        return entity.getInt("rowCount") > 0;
    }

    public static void main(String[] args) {
        User user = HrmUtil.getUser(29552);
        if(user == null){
            System.out.println("user is null");
        } else {
            System.out.println(user);
        }
    }
}
